package org.yoyo.coordinatearray;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import tiled.core.Map;
import tiled.core.MapLayer;
import tiled.core.TileLayer;
import tiled.core.TileSet;
import tiled.io.MapWriter;
import tiled.io.PluginLogger;

public class ArrayWriter implements MapWriter{

	private PluginLogger logger;
	 private Writer writer;


	@Override
	public void writeMap(Map map, String filename) throws Exception {
		writeMap(map,new FileOutputStream(filename));
	}

	@Override
	public void writeMap(Map map, OutputStream out) throws Exception {
		writer = new OutputStreamWriter(out);
		
		writeString("Auto Generated by J2meArrayWriter\n");
		
		Iterator layerIterator = map.getLayers();
		while (layerIterator.hasNext()) {
            MapLayer ml = (MapLayer) layerIterator.next();
            if (ml instanceof TileLayer) {
            	
            	TileLayer tl = (TileLayer)ml;
            	int width = map.getWidth();
            	int height = map.getHeight();
            	
            	writeString("arrays={\n");
            	
            	for(int i=0;i<width;i++)
            	{
            		writeString("{");
            		for(int j=0;j<height;j++){
            			writeString((tl.getTileAt(i, j)==null?0:tl.getTileAt(i, j).getId())+ (j==height-1?"":","));
            		}
            		
            		writeString("}"+ (i==width-1?"":",")+"\n");
            	}
            	writeString("}");
            }
            writeString(ml.toString());
        }
		
		writer.flush();
        writer = null;
	}

	@Override
	public void writeTileset(TileSet set, String filename) throws Exception {
		writeTileset(set,new FileOutputStream(filename));
	}

	@Override
	public void writeTileset(TileSet set, OutputStream out) throws Exception {
		logger.error("Tilesets are not supported!");
	}

    private void writeProperties(Properties props) throws IOException
    {
        if (!props.isEmpty()) {
            writeString( "properties\n" );
            for (Enumeration keys = props.keys(); keys.hasMoreElements();) {
                String key = (String)keys.nextElement();
                writeString("key:" + key + "==value:"+ props.getProperty(key) + "\n");
            }
            writeString("\n");
        }
    }
	private void writeString(String str) throws IOException{
		writer.write(str);
	}
	private void writeString(int num) throws IOException{
		writeString(Integer.toString(num));
	}
	
	
	
	@Override
	public String getDescription() {		
		  return
          "J2ME Coordinate Array Export Plugin\n" +
          "(c) 2010 You\n" +
          "msnvip@msn.cn\n" +
          "Released under the terms of the GPLv2.\n";
	}

	@Override
	public String getFilter() throws Exception {
		return "*.txt";
	}

	@Override
	public String getName() {
		return "Tiled J2me Array exporter";
	}

	@Override
	public String getPluginPackage() {
		return "Tiled Array Writer";
	}

	@Override
	public void setLogger(PluginLogger logger) {
		this.logger = logger;
	}

	@Override
	public boolean accept(File pathname) {
		try {
			String path = pathname.getCanonicalPath();
			if (path.endsWith(".txt")) {
				return true;
			}
		} catch (IOException e) {
		}
		return false;
	}
	public PluginLogger getLogger() {
		return logger;
	}
}
